<template>
  <div>
    <h1>添加讲师</h1>
    <el-row>
      <el-col :span="20">
        <el-form
          status-icon
          ref="teacher"
          label-width="200px"
          class="demo-ruleForm"
        >
          <el-form-item label="讲师姓名" required>
            <el-input v-model.trim="teacher.name"></el-input>
          </el-form-item>
          <el-form-item label="讲师排序"
            ><el-input-number
              v-model="teacher.sort"
              :min="1"
              :max="10"
              label="描述文字"
            ></el-input-number
          ></el-form-item>
          <el-form-item label="讲师头衔">
            <el-select v-model="teacher.level" placeholder="头衔">
              <el-option label="高级讲师" value="1"></el-option>
              <el-option label="首席讲师" value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="讲师资历" required>
            <el-input v-model.trim="teacher.career"></el-input>
          </el-form-item>
          <el-form-item label="讲师简介">
            <el-input
              v-model="teacher.intro"
              type="textarea"
              :rows="10"
            ></el-input>
          </el-form-item>
          <el-form-item label="讲师头像">
            <!-- 上传 -->
            <el-upload
              class="avatar-uploader"
              :action="BASE_API+'/ossService/fileOss/upload'"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              filed="file"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="teacher.avatar" :src="teacher.avatar" class="avatar"/>
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="addTeacher">保存讲师</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import teacher from "@/api/edu/teacher";
export default {
  data() {
    return {
      BASE_API:process.env.VUE_APP_BASE_API,
      teacher: {
        id: null,
        name: "",
        intro: null,
        career: "",
        level: "1",
        avatar: null,
        sort: null,
      },
    };
  },
  methods: {
    //添加
    addTeacher() {
      if (this.rules()) {
        //判断为修改还是添加
        if (this.teacher.id == null) {
          teacher.addTeacher(this.teacher).then((Response) => {
            this.$router.push("/teacher/table");
            this.$message({
              type: "success",
              message: "添加成功!",
            });
          });
        } else {
          this.updateteacher();
        }
      }
    },
    //规则
    rules() {
      if (this.teacher.name == "") {
        this.$message({
          type: "warning",
          message: "讲师姓名必填!",
        });
        return false;
      }
      if (this.teacher.career == "") {
        this.$message({
          type: "warning",
          message: "讲师资历必填!",
        });
        return false;
      }
      return true;
    },
    //修改
    updateteacher() {
      teacher.updateTeacher(this.teacher).then((Response) => {
        //路由回转
        this.$router.push("/teacher/table");
        //提示消息
        this.$message({
          type: "success",
          message: "修改成功!",
        });
      });
    },
    //文件上传成功执行
    handleAvatarSuccess(res, file) {
      // this.teacher.avatar = URL.createObjectURL(file.raw);
      this.teacher.avatar =res.data.url
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
  mounted() {
    if (this.$route.params && this.$route.params.id) {
      const id = this.$route.params.id;
      teacher.getTeacher(id).then((Response) => {
        this.teacher = Response.data.teacher;
      });
    }
  },
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>